
#import "AnalysisObject.h"

@implementation AnalysisObject

// Gobal Variables

	char buffer[800];     // Text buffer

// AWAKEFROMNIB
// This is one of the last methods executed after the nib file is loaded.
// This is the BEST way I know how to initialize an object and jump into
// a method after the nib file was loaded, because you are all the methods
// and variables needed can be used. (Unlike init which is not compeletly
// ready when that message is sent
- awakeFromNib
{
 
	[AnalysisWindow makeKeyAndOrderFront:NULL];
	[StockPrice selectText:self];

    	return self;
}

- Calc:sender
{

	//Assign Misc Variables
    	char dummyString[20];
    	int WhichOne;
	float stockprice;
    	float numberofshares;
    	float totalstockprice;
    	float totalcommission;
	float totalcommissioncharges;
    	float total;
	float netamountpershare;
	float totalcommpershare;
	float netprofitlost;
	float netprofitlostpershare;
	float stockprice2;
    	float numberofshares2;
    	float totalstockprice2;
    	float totalcommission2;
    	float total2;
	float netamountpershare2;
	float breakeven;
	float neededvalue;
	float neededvalue2;
        float buycommission;
        float netamount;

	//Assign Values for Buy section
	stockprice = [StockPrice floatValue];
	numberofshares = [NumberOfShares floatValue];
        [NumberOfShares2 setFloatValue:numberofshares];
	
	//Assign Values for Sell section
	stockprice2 = [StockPrice2 floatValue];
	numberofshares2 = [NumberOfShares2 floatValue];


	// Take the value in Stock Price * Number of Shares and display
	// the result in the Total Stock Price textfield
	
	// For Buy section
	totalstockprice = [StockPrice floatValue] * [NumberOfShares floatValue];
	sprintf(dummyString, "%.2f", totalstockprice); 
	[TotalStockPrice setStringValue:dummyString];// Display result

	// For Sell Section
	totalstockprice2 = [StockPrice2 floatValue] * [NumberOfShares2 floatValue];
	sprintf(dummyString, "%.2f", totalstockprice2); 
	[TotalStockPrice2 setStringValue:dummyString];// Display result

	// COMMISSION RATE SECTION **************************


	// Figure out the commission rate and then do the calculations

	// Checks if the stock price is below Stock1 if yes then
	// calculates the commission using Comm1

	// For Sell Section
	if ([StockPrice floatValue] <= [Stock2 floatValue]) totalcommission = (([StockPrice floatValue] * [NumberOfShares floatValue]) * ([Comm1 floatValue] / 100));

	// For Buy Section
	if ([StockPrice2 floatValue] <= [Stock2 floatValue]) totalcommission2 = (([StockPrice2 floatValue] * [NumberOfShares2 floatValue]) * ([Comm1 floatValue] / 100));

	// Checks if the stock price is >=Stock3 and <= Stock4 if yes then
	// calculates the commission by adding Comm2 and multiplying
	// Comm3 by the number of shares

	// For Buy section
	if ([StockPrice floatValue] >= [Stock3 floatValue] && [StockPrice floatValue] <= [Stock4 floatValue]) totalcommission = ([Comm2 floatValue] + ([NumberOfShares floatValue] * [Comm3 floatValue]));

	// For Sell section
	if ([StockPrice2 floatValue] >= [Stock3 floatValue] && [StockPrice2 floatValue] <= [Stock4 floatValue]) totalcommission2 = ([Comm2 floatValue] + ([NumberOfShares2 floatValue] * [Comm3 floatValue]));

	// Checks if the stock price is >=Stock5 and <= Stock6 if yes then
	// calculates the commission by adding Comm4 and multiplying
	// Comm5 by the number of shares

	// For Buy section
	if ([StockPrice floatValue] >= [Stock5 floatValue] && [StockPrice floatValue] <= [Stock6 floatValue]) totalcommission = ([Comm4 floatValue] + ([NumberOfShares floatValue] * [Comm5 floatValue]));

	// For Sell section
	if ([StockPrice2 floatValue] >= [Stock5 floatValue] && [StockPrice2 floatValue] <= [Stock6 floatValue]) totalcommission2 = ([Comm4 floatValue] + ([NumberOfShares2 floatValue] * [Comm5 floatValue]));

	// Checks if the stock price is >=Stock7 and <= Stock8 if yes then
	// calculates the commission by adding Comm6 and multiplying
	// Comm7 by the number of shares

	// For Buy section
	if ([StockPrice floatValue] >= [Stock7 floatValue] && [StockPrice floatValue] <= [Stock8 floatValue]) totalcommission = ([Comm6 floatValue] + ([NumberOfShares floatValue] * [Comm7 floatValue]));

	// For Sell section
if ([StockPrice2 floatValue] >= [Stock7 floatValue] && [StockPrice2 floatValue] <= [Stock8 floatValue]) totalcommission2 = ([Comm6 floatValue] + ([NumberOfShares2 floatValue] * [Comm7 floatValue]));

	// Checks if the stock price is >=Stock9 and <= Stock10 if yes then
	// calculates the commission by adding Comm8 and multiplying
	// Comm9 by the number of shares

	// For Buy section
	if ([StockPrice floatValue] >= [Stock9 floatValue] && [StockPrice floatValue] <= [Stock10 floatValue]) totalcommission = ([Comm8 floatValue] + ([NumberOfShares floatValue] * [Comm9 floatValue]));

	// For Sell section
if ([StockPrice2 floatValue] >= [Stock9 floatValue] && [StockPrice2 floatValue] <= [Stock10 floatValue]) totalcommission2 = ([Comm8 floatValue] + ([NumberOfShares2 floatValue] * [Comm9 floatValue]));

	// Checks if the stock price is >=Stock11 and <= Stock12 if yes then
	// calculates the commission by adding Comm10 and multiplying
	// Comm11 by the number of shares

	// For Buy section
	if ([StockPrice floatValue] >= [Stock11 floatValue] && [StockPrice floatValue] <= [Stock12 floatValue]) totalcommission = ([Comm10 floatValue] + ([NumberOfShares floatValue] * [Comm11 floatValue]));

	// For Sell section
	if ([StockPrice2 floatValue] >= [Stock11 floatValue] && [StockPrice2 floatValue] <= [Stock12 floatValue]) totalcommission2 = ([Comm10 floatValue] + ([NumberOfShares2 floatValue] * [Comm11 floatValue]));

	// Checks if the stock price is >=Stock13 if yes then
	// calculates the commission by adding Comm11 and multiplying
	// Comm13 by the number of shares

	// For Buy section
if ([StockPrice floatValue] >= [Stock13 floatValue]) totalcommission = ([Comm12 floatValue] + ([NumberOfShares floatValue] * [Comm13 floatValue]));

	// For Sell Section
if ([StockPrice2 floatValue] >= [Stock13 floatValue]) totalcommission2 = ([Comm12 floatValue] + ([NumberOfShares2 floatValue] * [Comm13 floatValue]));


	// DISPLAY SECTION **************************

	// Assign variables
	totalstockprice = [TotalStockPrice floatValue];
	totalstockprice2 = [TotalStockPrice2 floatValue];

	// Transactions with principal values less than Principal Value 
        //will be charged a flat fee of FlatFee
        if ([TotalStockPrice floatValue] <= [PrincipalValue floatValue]) totalcommission = [FlatFee floatValue];
        if ([TotalStockPrice2 floatValue] <= [PrincipalValue floatValue]) totalcommission2 = [FlatFee floatValue];


	// Assigns the value of the currently selected radio button
 	// using tag as a reference and calculate Net Amount
   	WhichOne = [RadioButton selectedTag]; 
	if (WhichOne == 2)  totalcommission = totalcommission -(totalcommission * ([TeleMaxInput floatValue] / 100));
	if (WhichOne == 2)  totalcommission2 = totalcommission2 -(totalcommission2 * ([TeleMaxInput floatValue] / 100));

	total = totalstockprice + totalcommission;
	total2 = totalstockprice2 - totalcommission2;

	
	// Display Total Commission 
	// Convert float to a string
	sprintf(dummyString, "%.2f", totalcommission); 
	[TotalCommission setStringValue:dummyString];// Display result
	sprintf(dummyString, "%.2f", totalcommission2); 
	[TotalCommission2 setStringValue:dummyString];// Display result

	// Display Net Amount
	sprintf(dummyString, "%.2f", total); // Convert float to a string
	[Total setStringValue:dummyString];// Display result
	sprintf(dummyString, "%.2f", total2); // Convert float to a string
	[Total2 setStringValue:dummyString];// Display result

	// Calculate Net Amount Per Share
	// Number of Shares / Net Amount = Net Amount per Share
	netamountpershare =  [Total floatValue] / [NumberOfShares floatValue];
	netamountpershare2 =  [Total2 floatValue] / [NumberOfShares2 floatValue];
	// Display Net Amount per Share
	sprintf(dummyString, "%.2f", netamountpershare);
	[NetAmountPerShare setStringValue:dummyString];// Display result
	[NetAmountPerShare display];
	sprintf(dummyString, "%.2f", netamountpershare2);
	[NetAmountPerShare2 setStringValue:dummyString];// Display result
	[NetAmountPerShare2 display];

// ANALYSIS ***************************

	// Calc and Display Result for Share Price Difference
	[SharePriceDifference setFloatValue:[StockPrice2 floatValue]-[StockPrice floatValue]];
	
	// If Negative value text is RED
	if ([SharePriceDifference floatValue] < 0) [SharePriceDifference setTextColor:NX_COLORRED];
	
	// If Positive value text is BLACK
	if ([SharePriceDifference floatValue] >= 0) [SharePriceDifference setTextColor:NX_COLORBLACK];

	// Calc and Display Result for Total Commission Charges
	totalcommissioncharges = [TotalCommission floatValue] + [TotalCommission2 floatValue];
	sprintf(dummyString, "%.2f", totalcommissioncharges);
	[TotalCommissionCharges setStringValue:dummyString];// Display result
	[TotalCommissionCharges display];

	// Calc and Display Total Commission Charge per Share
	totalcommpershare = [TotalCommissionCharges floatValue] / 	[NumberOfShares floatValue];
	sprintf(dummyString, "%.5f", totalcommpershare);
	[TotalCommPerShare setStringValue:dummyString];// Display result
	[TotalCommPerShare display];

	// Calc and Display Net Profit or Lost
	netprofitlost = [Total2 floatValue] - [Total floatValue];

	// If Negative value text is RED
	if (netprofitlost < 0) [NetProfitLost setTextColor:NX_COLORRED];

	// If Positive value text is BLACK
	if (netprofitlost >= 0) [NetProfitLost setTextColor:NX_COLORBLACK];
	sprintf(dummyString, "%.2f", netprofitlost);
	[NetProfitLost setStringValue:dummyString];// Display result
	[NetProfitLost display];

	
	// Calc and Display Net Profit or Lost per Share
	netprofitlostpershare = [NetProfitLost floatValue] / [NumberOfShares floatValue];

	// If Negative value text is RED	
	if (netprofitlostpershare < 0) [NetProfitLostPerShare setTextColor:NX_COLORRED];

	// If Positive value text is BLACK
	if (netprofitlostpershare >= 0) [NetProfitLostPerShare setTextColor:NX_COLORBLACK];

sprintf(dummyString, "%.5f", netprofitlostpershare);
[NetProfitLostPerShare setStringValue:dummyString];// Display result
[NetProfitLostPerShare display];


	// BREAKEVEN ************************
	// Breakeven is simply getting the "Buy" Net Amount per Share
        // Equal to the "Sell" Net Amount per Share.
	//
	// This is done by increasing the value of the stock price by
	// 0.05 then multiply by the number of shares, less the commission 
	// charge then dividing by number of shares.
	// If this value is equal to "Buy" Net Amount per Share
	// Then your stock price is the correct one for breakeven.
	

        buycommission = [TotalCommission floatValue];
	numberofshares = [NumberOfShares floatValue];
	netamount = [Total floatValue];	
	breakeven = [StockPrice floatValue];

	// Start of the BREAKEVEN GOTO LOOP 
	BREAKEVENstart:breakeven = breakeven + 0.01;

	// COMMISSION RATE SECTION  FOR BREAKEVEN **********************

	// Figure out the commission rate and then do the calculations

	// Checks if the stock price is below Stock1 if yes then
	// calculates the commission using Comm1

	// For Sell Section
	if (breakeven <= [Stock2 floatValue]) totalcommission = ((breakeven * [NumberOfShares floatValue]) * ([Comm1 floatValue] / 100));

	// Checks if the stock price is >=Stock3 and <= Stock4 if yes then
	// calculates the commission by adding Comm2 and multiplying
	// Comm3 by the number of shares

	// For Sell section
	if (breakeven >= [Stock3 floatValue] && breakeven <= [Stock4 floatValue]) totalcommission = ([Comm2 floatValue] + ([NumberOfShares floatValue] * [Comm3 floatValue]));

	// Checks if the stock price is >=Stock5 and <= Stock6 if yes then
	// calculates the commission by adding Comm4 and multiplying
	// Comm5 by the number of shares

	// For Sell section
	if (breakeven >= [Stock5 floatValue] && breakeven <= [Stock6 floatValue]) totalcommission = ([Comm4 floatValue] + ([NumberOfShares floatValue] * [Comm5 floatValue]));

	// Checks if the stock price is >=Stock7 and <= Stock8 if yes then
	// calculates the commission by adding Comm6 and multiplying
	// Comm7 by the number of shares

	// For Sell section
	if (breakeven >= [Stock7 floatValue] && breakeven <= [Stock8 floatValue]) totalcommission = ([Comm6 floatValue] + ([NumberOfShares floatValue] * [Comm7 floatValue]));

	// Checks if the stock price is >=Stock9 and <= Stock10 if yes then
	// calculates the commission by adding Comm8 and multiplying
	// Comm9 by the number of shares

	// For Sell section
	if (breakeven >= [Stock9 floatValue] && breakeven <= [Stock10 floatValue]) totalcommission = ([Comm8 floatValue] + ([NumberOfShares floatValue] * [Comm9 floatValue]));

	// Checks if the stock price is >=Stock11 and <= Stock12 if yes then
	// calculates the commission by adding Comm10 and multiplying
	// Comm11 by the number of shares

	// For Sell section
	if (breakeven >= [Stock11 floatValue] && breakeven <= [Stock12 floatValue]) totalcommission = ([Comm10 floatValue] + ([NumberOfShares floatValue] * [Comm11 floatValue]));

	// Checks if the stock price is >=Stock13 if yes then
	// calculates the commission by adding Comm11 and multiplying
	// Comm13 by the number of shares

	// For Sell Section
	if (breakeven >= [Stock13 floatValue]) totalcommission = ([Comm12 floatValue] + ([NumberOfShares floatValue] * [Comm13 floatValue]));

	// Transactions with principal values less than Principal Value 
        //will be charged a flat fee of FlatFee
        if ((breakeven*[NumberOfShares floatValue]) < [PrincipalValue floatValue]) totalcommission = [FlatFee floatValue];

	// The target value your trying to find.	
	neededvalue = [NetAmountPerShare floatValue];

	// Calculating the Net Amount per Share based on the breakeven
	// value and the commission charges for that value divided
	// by the number of shares
	neededvalue2 = ((breakeven * numberofshares) - totalcommission) / numberofshares; 

	// Checking to see if Net Amount per Share for breakeven
	// is smaller then the target value Net Amount per Share
	// in the Buy box IF SO then increase the breakeven value
	if (neededvalue2 < neededvalue) goto BREAKEVENstart;
	
	// Display breakeven value in the BREAKEVEN, Analysis box
	sprintf(dummyString, "%.2f", breakeven);
	[BreakEven setStringValue:dummyString];// Display result
	[BreakEven display];
	
	[StockPrice selectText:self];


    return self;
}



- DirectTrading:sender
{
    return self;
}


- PrintCommSchedule:sender
{

	[CommissionWindow printPSCode:self];

    return self;
}

- PrintAll:sender
{

	// Clears the MyScrollView scrollView
	[[MyScrollView docView] setText:""];

	[ReportWindow makeKeyAndOrderFront:NULL];

	// Header
 
	sprintf(buffer,"\n                *** Emerald ***\n");
	[self appendText:sender];

	sprintf(buffer,"The NEXTSTEP/OPENSTEP Financial Tool.\n");
	[self appendText:sender];

        sprintf(buffer,"Canadian Equities Analysis Report\n\n");
        [self appendText:sender];

	// Buy Report

        sprintf(buffer,"** Buy **\n\n");
        [self appendText:sender];

	sprintf(buffer,"Stock Price: %.2f\n", [StockPrice floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Number of Shares: %.0f\n", [NumberOfShares floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Value: %.2f\n", [TotalStockPrice floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Commission: %.2f\n", [TotalCommission floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Net Amount: %.2f\n", [Total floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Net Amount per Share: $%.2f\n\n", [TotalCommPerShare floatValue]);
	[self appendText:sender];


	// Sell Report

        sprintf(buffer,"** Sell **\n\n");
        [self appendText:sender];

	sprintf(buffer,"Stock Price: %.2f\n", [StockPrice2 floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Number of Shares: %.2f\n", [NumberOfShares2 floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Value: %.2f\n", [TotalStockPrice2 floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Commission: %.2f\n", [TotalCommission2 floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Net Amount: %.2f\n", [Total2 floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Net Amount per Share: %.2f\n\n", [NetAmountPerShare2 floatValue]);
	[self appendText:sender];
    

	// Analysis Report

        sprintf(buffer,"** Analysis **\n\n");
        [self appendText:sender];

	sprintf(buffer,"Share Price Difference: %.2f\n", [SharePriceDifference floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Total Commission Charges: %.2f\n", [TotalCommissionCharges floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Total Commission Charges per share: %.5f\n", [TotalCommPerShare floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Net Profit or lost per Share: %.5f\n", [NetProfitLostPerShare floatValue]);
	[self appendText:sender];

	sprintf(buffer,"Net Profit or lost: %.2f\n", [NetProfitLost floatValue]);
	[self appendText:sender];

	sprintf(buffer,"*Breakeven* Price per Share: %.2f\n", [BreakEven floatValue]);
	[self appendText:sender];


    return self;
}



// APPENDTEXT
- appendText:sender
// This method appends a string called buffer to the end 
// of AmortDisplay scrollView
{
    	int HowLongIsTheText;
	
	document = [MyScrollView docView];
        
	// Determines how many characters are in the 
        // AmortDisplay scrollView document
	HowLongIsTheText = [document textLength]; 
        
        // Selects and empty selection, which in fact places
        // the cursor at the end of the document.
	[document setSel:HowLongIsTheText:HowLongIsTheText];
        
	// Replaces the selection (in this case it is empty) 
        // with buffer text string.
	[document replaceSel:buffer]; 

	return self; //normal return
}

- PrintCalc:sender
{

    [[MyScrollView docView] printPSCode:self];

    return self;
}

- PrintBuy:sender
{

	[BuyBox printPSCode:self];

    return self;
}

- PrintSell:sender
{

	[SellBox printPSCode:self];

    return self;
}

- TeleMax:sender
{
    return self;
}


@end
